Strict

Import agfx

Global myTmpScreen:TTmpScreen

Class TAgfx_test_app Extends TFramework

	Method OnCreate:Int()
		
		Super.OnCreate()
				
		DISPLAY.SetVirtualSize(480,320)
		myTmpScreen = New TTmpScreen		
		myTmpScreen.Prepare()
		
		
		Return 0
	End
	
End

Class TmyPlayerControl Extends TGestureRecorder

	Field obj:TBox2DBody

	Method New(o:TBox2DBody)
		Super.New()
		Self.obj = o
	End
	Method OnRelease:Void()
	
		Local last:Int = Self.pointArray.Length()
		
		Local gs_x:Int = Self.pointArray[0].position.x
		Local gs_y:Int = Self.pointArray[0].position.y
		Local ge_x:Int = Self.pointArray[last-1].position.x
		Local ge_y:Int = Self.pointArray[last-1].position.y

		Local v_start:TVector2 = New TVector2(gs_x,gs_y)
		Local v_end:TVector2 = New TVector2(ge_x,ge_y)

		Local v_direction:TVector2 = VecSub(v_end,v_start)
		Local speed:Float = v_direction.Length()*0.001
		If speed>0.15 Then speed = 0.15
		
		v_direction = VecNormalize(v_direction)
		v_direction = VecMult(v_direction,speed)
		
		
		Self.obj.body.ApplyImpulse(New b2Vec2(v_direction.x, v_direction.y), obj.body.GetWorldCenter())
		'Self.obj.body.ApplyImpulse(New b2Vec2(0, -3), obj.body.GetWorldCenter())
		
	End Method

End Class

Class TTmpScreen Extends TScreen
	

	Field level_data:		TTiled_Map
	Field level_renderer:	TTiled_Renderer
	Field level_physics:	TTiled_Physics
	
	Field player_sprite:	TSprite
	Field player_control:	TmyPlayerControl
	Field player_body:		TBox2DBody
	
	Field off_x:Int=0
	Field off_y:Int=0
	
	Field slider:TGUISlider
	
	' START when app is created
	Method Start:Void()
	
		DEBUG = False
		
		' [0] load resources FIRST !!!!
		
		'RESOURCE.Load("resource_320x480.xml")
		
		' [1] create level data
		Self.level_data 		= New TTiled_Map()
		Self.level_data.Load("agfx_platform_test.xml")

		' [2] define layers order to render
		Self.level_renderer 	= New TTiled_Renderer(self.level_data)

		
		Self.level_renderer.AddRenderLayersOrder("TileLayer_parallax")
		Self.level_renderer.AddRenderLayersOrder("TileLayer_bkg")
		Self.level_renderer.AddRenderLayersOrder("TileLayer_middle")
		' Self.level_renderer.AddRenderLayersOrder("Objects_Physics")  <- momentalne netreba renderovat
		Self.level_renderer.AddRenderLayersOrder("TileLayer_front")
		Self.level_renderer.AddRenderLayersOrder("TileLayer_hud")
		

		'[3] create physics world
		Self.level_physics = New TTiled_Physics(Self.level_data,True)

		'[4] add user defined collision shapes from tileset
		Self.level_physics.LoadCollisionShapeTemplates("agfx_collision")
		
		'[5] build physics world from level_data
		Self.level_physics.BuildCollisionWorld("TileLayer_collision")
		
		'[6] Create Box2D player control from Object info_player_start
		Local player_data:TTiled_ObjectGroupObject = Self.level_data.GetObjectGroupByName("Objects_Physics").GetObjectByType("info_player_start")
		Local use_box2d:Bool = False
		
		' check using box2d
		If player_data.properties.Has("box2d") Then
			use_box2d = player_data.properties.Get("box2d").ToBool()
		End If

		'if box2d enabled
		If use_box2d Then
			
			' create fixture
			Local player_fixture:b2FixtureDef = New b2FixtureDef()
			
			player_fixture.density 		= player_data.properties.Get("b2d_density").ToFloat()
			player_fixture.restitution 	= player_data.properties.Get("b2d_restitution").ToFloat()
			player_fixture.friction 	= player_data.properties.Get("b2d_friction").ToFloat()
			
			
			Local sprite_name:String	= player_data.properties.Get("sprite_name").ToString()
			Local sprite_image:String 	= player_data.properties.Get("sprite_image").ToString()
			
			' create box2d player collision body
			Self.player_body = level_physics.collision.CreateDynamicBox(sprite_name,DISPLAY.VSCREEN_HALF_WIDTH,DISPLAY.VSCREEN_HALF_HEIGHT,player_data.tilewidth/2.0,player_data.tileheight/2.0,player_fixture)
		
			' disable sleep for body
			Self.player_body.body.SetBullet(True)
			' disable body rotating 
			Self.player_body.body.m_sweep.a = 0
			
			' load player anim
			IMAGES.LoadAnim(sprite_image,player_data.width,player_data.height,4,null,True)
			
			' create player sprite
			Self.player_sprite = New TSprite(sprite_name,IMAGES.Find(StripExt(sprite_image)),DISPLAY.VSCREEN_HALF_WIDTH,DISPLAY.VSCREEN_HALF_HEIGHT-8)
			
			' create player controll
			Self.player_control = New TmyPlayerControl(Self.player_body)
			Self.player_control.draw_on=True
			Self.player_control.mode=GM_LINE
		End If
		 
		
		IMAGES.Load("slider_left.png", "SLIDER_LEFT", True)	
		IMAGES.Load("slider_middle.png", "SLIDER_MIDDLE", True)	
		IMAGES.Load("slider_right.png", "SLIDER_RIGHT", True)	
		IMAGES.Load("slider_button.png", "SLIDER_BUTTON", True)	

		
		Self.slider = New TGUISlider("Slider","SLIDER_LEFT","SLIDER_MIDDLE","SLIDER_RIGHT","SLIDER_BUTTON",32,32,320,8,0,1,0)
		
	End
	
	' main app logic loop update
	Method Update:Void()
	
		
		#If TARGET="android" Then
		
			If MOUSE.Hit(0) And MOUSE.Hit(1) Then 
				Self.player_body.body.ApplyImpulse(New b2Vec2(0, -0.1), Self.player_body.body.GetWorldCenter())
			ElseIf MOUSE.Down(0) And MOUSE.x<DISPLAY.VSCREEN_HALF_WIDTH Then 
				Self.player_body.body.m_linearVelocity.x = -2
			ElseIf MOUSE.Down(0) And MOUSE.x>DISPLAY.VSCREEN_HALF_WIDTH Then 
				Self.player_body.body.m_linearVelocity.x = 2
			EndIf
			
			
		#Else
			
			If KeyDown(KEY_LEFT) Then Self.player_body.body.m_linearVelocity.x = -2
			If KeyDown(KEY_RIGHT) Then Self.player_body.body.m_linearVelocity.x = 2
			If KeyHit(KEY_UP) Then Self.player_body.body.ApplyImpulse(New b2Vec2(0, -0.1), Self.player_body.body.GetWorldCenter())
		
		#End
		
		Self.player_body.body.m_sweep.a = 0.0
		
		#Rem
		If KeyDown(KEY_LEFT) off_x = off_x - 2
		If KeyDown(KEY_RIGHT) off_x = off_x + 2
		If KeyDown(KEY_UP) off_y = off_y - 2
		If KeyDown(KEY_DOWN) off_y = off_y + 2
		#End
		
		Self.player_sprite.Update()				
		Self.level_physics.Update()
		
		
		Self.off_x = Self.player_body.body.GetPosition().x*100
		Self.off_y = Self.player_body.body.GetPosition().y*100
		Self.level_data.SetPosition(Self.off_x-DISPLAY.VSCREEN_HALF_WIDTH,Self.off_y-DISPLAY.VSCREEN_HALF_HEIGHT)
		
		Self.level_renderer.Update()
		
	End
	
	' main app render loop
	Method Render:Void()
	
		Cls
		Self.player_control.Render()
		Self.level_renderer.Render()
		Self.player_sprite.Draw()
		Self.level_physics.Render()
		Self.slider.Render()
	End
End
